package com.sbpm.crmbpm.client.deprecforms;

import com.sbpm.crmbpm.shared.data.InsuranceRequestInfo;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.form.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by U_M0PQL on 16.11.2014.
 */
public abstract class InsuranceRequestForm extends ProcessForm
{
    protected TextField clientName;
    protected HtmlEditor description;
    protected CheckBox isVip;
    protected CheckBox verified;
    protected DateField eventDate;
    protected TextField damageSum;
    protected FieldLabel damageSumLabel;
    protected FieldLabel clientNameLabel;
    protected FieldLabel eventDateLabel;
    protected FieldLabel descriptionLabel;
    protected CheckBox expertisePassed;
    protected CheckBox approvedByLegal;
    protected CheckBox approvedByCompliance;
    protected CheckBox approvedByDirector;

    @Override
    public void createForm(FramedPanel panel)
    {
        FieldSet fieldSet = new FieldSet();
        fieldSet.setHeadingText( "Обработка обращения по страховому случаю" );
        fieldSet.setCollapsible( false );
        panel.add( fieldSet );

        VerticalLayoutContainer p = new VerticalLayoutContainer();
        fieldSet.add( p );

        p.add( createLabel( getTaskDescription() ), new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ) );


        clientName = new TextField();
        clientName.setAllowBlank( false );
        clientNameLabel = new FieldLabel( clientName, "Клиент" );
        p.add( clientNameLabel, new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ) );

        enableClientName( false );

        isVip = new CheckBox();
        isVip.setBoxLabel( "ВИП-клиент" );
        /*isVip.setReadOnly( true );
        isVip.setEnabled( false );*/
        p.add( isVip, new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ) );
        enableIsVip( false );

        eventDate = new DateField();
        eventDate.setAllowBlank( false );
        eventDateLabel = new FieldLabel( this.eventDate, "Дата страхового случая" );
        p.add( eventDateLabel, new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ) );
        enableEventDate( false );

        description = new HtmlEditor();
        description.setWidth( 625 );
        descriptionLabel = new FieldLabel( description, "Описание страхового случая" );
        p.add( descriptionLabel, new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ));
        enableDescription( false );

        damageSum = new TextField();
        damageSum.setAllowBlank( false );
        damageSumLabel = new FieldLabel( damageSum, "Сумма ущерба" );
        p.add( damageSumLabel, new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ) );
        enableDamageSum( false );

        verified = new CheckBox();
        verified.setBoxLabel( "Данные введены корректно" );
        p.add( verified, new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ) );
        enableVerified( false );

        expertisePassed = new CheckBox();
        expertisePassed.setBoxLabel( "Экспертиза пройдена" );
        p.add( expertisePassed, new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ) );
        enableExpertisePassed( false );

        approvedByLegal = new CheckBox();
        approvedByLegal.setBoxLabel( "Согласовано" );
        p.add( approvedByLegal, new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ) );
        enableApprovedByLegal( false );

        approvedByCompliance = new CheckBox();
        approvedByCompliance.setBoxLabel( "Согласовано" );
        p.add( approvedByCompliance, new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ) );
        enableApprovedByCompliance( false );

        approvedByDirector = new CheckBox();
        approvedByDirector.setBoxLabel( "Согласовано" );
        p.add( approvedByDirector, new VerticalLayoutContainer.VerticalLayoutData( 1, -1 ) );
        enableApprovedByDirector( false );

        applyVisibility();

        setWidth( 800 );
    }

    public abstract void applyVisibility();

    @Override
    public Map<String, Object> getData() {
        Map<String, Object> data = new HashMap<>();
        data.put(InsuranceRequestInfo.CLIENT_NAME, clientName.getValue() );
        data.put( InsuranceRequestInfo.INSURED_EVENT_DESCRIPTION, description.getValue() );
        data.put( InsuranceRequestInfo.VIP, isVip.getValue() );
        data.put( InsuranceRequestInfo.EVENT_DATE, eventDate.getValue() );
        if( damageSum.getValue() != null && damageSum.getValue().trim().length() > 0 ) {
            BigDecimal sum = new BigDecimal( damageSum.getValue() );
            sum = sum.setScale( 2, BigDecimal.ROUND_CEILING );
            data.put(InsuranceRequestInfo.DAMAGE_SUM, sum );
        }
        data.put( InsuranceRequestInfo.VERIFIED_REQUEST, verified.getValue() );
        data.put( InsuranceRequestInfo.EXPERTISE_PASSED, expertisePassed.getValue() );
        data.put( InsuranceRequestInfo.APPROVED_BY_LEGAL, approvedByLegal.getValue() );
        data.put( InsuranceRequestInfo.APPROVED_BY_COMPLIANCE, approvedByCompliance.getValue() );
        data.put( InsuranceRequestInfo.APPROVED_BY_DIRECTOR, approvedByDirector.getValue() );
        return data;
    }

    @Override
    public void setData(Map<String, Object> data)
    {
        clientName.setValue((String) data.get(InsuranceRequestInfo.CLIENT_NAME));
        description.setValue( ( String ) data.get( InsuranceRequestInfo.INSURED_EVENT_DESCRIPTION ) );
        isVip.setValue( ( Boolean ) data.get( InsuranceRequestInfo.VIP ) );
        eventDate.setValue( ( Date ) data.get( InsuranceRequestInfo.EVENT_DATE ) );
        //setVerifiedRequest((Boolean) data.get(InsuranceRequestInfo.VERIFIED_REQUEST));
        //setExpertisePassed((Boolean) data.get(InsuranceRequestInfo.EXPERTISE_PASSED));
        BigDecimal sum = ( BigDecimal )data.get(InsuranceRequestInfo.DAMAGE_SUM);
        if( sum != null )
        {
            damageSum.setValue( sum.toString() );
        }
        else
        {
            damageSum.setValue( "" );
        }
        verified.setValue( (Boolean) data.get(InsuranceRequestInfo.VERIFIED_REQUEST) );
        expertisePassed.setValue( (Boolean) data.get(InsuranceRequestInfo.EXPERTISE_PASSED) );
        approvedByLegal.setValue((Boolean) data.get(InsuranceRequestInfo.APPROVED_BY_LEGAL));
        approvedByCompliance.setValue( ( Boolean ) data.get( InsuranceRequestInfo.APPROVED_BY_COMPLIANCE ) );
        approvedByDirector.setValue( ( Boolean ) data.get( InsuranceRequestInfo.APPROVED_BY_DIRECTOR ) );
    }

    protected void enableDamageSum( boolean enable )
    {
        damageSum.setVisible( enable );
        damageSumLabel.setVisible( enable );
    }

    protected void enableClientName( boolean enable )
    {
        clientName.setVisible( enable );
        clientNameLabel.setVisible( enable );
    }

    protected void enableDescription( boolean enable )
    {
        description.setVisible( enable );
        descriptionLabel.setVisible( enable );
    }

    protected void enableEventDate( boolean enable )
    {
        eventDate.setVisible( enable );
        eventDateLabel.setVisible( enable );
    }

    protected void enableIsVip( boolean enable )
    {
        isVip.setVisible( enable );
    }

    protected void enableExpertisePassed( boolean enable )
    {
        expertisePassed.setVisible( enable );
    }

    protected void enableApprovedByLegal( boolean enable )
    {
        approvedByLegal.setVisible( enable );
    }

    protected void enableApprovedByCompliance( boolean enable )
    {
        approvedByCompliance.setVisible( enable );
    }

    protected void enableApprovedByDirector( boolean enable )
    {
        approvedByDirector.setVisible( enable );
    }

    protected void enableVerified( boolean enable )
    {
        verified.setVisible( enable );
    }


}
